<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的-blade-模板引擎" tabindex="-1"><a class="header-anchor" href="#laravel-的-blade-模板引擎"><span>Laravel 的 Blade 模板引擎</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#template-inheritance">模板继承</a>
<ul>
<li><a href="#defining-a-layout">定义布局</a></li>
<li><a href="#extending-a-layout">继承布局</a></li>
</ul>
</li>
<li><a href="#components-and-slots">Components &amp; Slots</a></li>
<li><a href="#displaying-data">显示数据</a>
<ul>
<li><a href="#blade-and-javascript-frameworks">Blade &amp; JavaScript 框架</a></li>
</ul>
</li>
<li><a href="#control-structures">流程控制</a>
<ul>
<li><a href="#if-statements">If 语句</a></li>
<li><a href="#switch-statements">Switch 语句</a></li>
<li><a href="#loops">循环</a></li>
<li><a href="#the-loop-variable">循环变量</a></li>
<li><a href="#comments">注释</a></li>
<li><a href="#php">PHP</a></li>
</ul>
</li>
<li><a href="#including-sub-views">引入子视图</a>
<ul>
<li><a href="#rendering-views-for-collections">为集合渲染视图</a></li>
</ul>
</li>
<li><a href="#stacks">堆栈</a></li>
<li><a href="#service-injection">服务注入</a></li>
<li><a href="#extending-blade">Blade 扩展</a>
<ul>
<li><a href="#custom-if-statements">自定义 If 语句</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Blade 是 Laravel 提供的一个简单而又强大的模板引擎。和其他流行的 PHP 模板引擎不同，Blade 并不限制你在视图中使用原生 PHP 代码。所有 Blade 视图文件都将被编译成原生的 PHP 代码并缓存起来，除非它被修改，否则不会重新编译，这就意味着 Blade 基本上不会给你的应用增加任何负担。Blade 视图文件使用 <code v-pre>.blade.php</code> 作为文件扩展名，被存放在 <code v-pre>resources/views</code> 目录。</p>
<h2 id="模板继承" tabindex="-1"><a class="header-anchor" href="#模板继承"><span>模板继承</span></a></h2>
<h3 id="定义布局" tabindex="-1"><a class="header-anchor" href="#定义布局"><span>定义布局</span></a></h3>
<p>Blade 的两个主要优点是 <em>模板继承</em> 和 <em>区块</em> 。为方便开始，让我们先通过一个简单的例子来上手。首先，我们来研究一个「主」页面布局。因为大多数 web 应用会在不同的页面中使用相同的布局方式，因此可以很方便地定义单个 Blade 布局视图：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 文件保存于 resources<span class="token operator">/</span>views<span class="token operator">/</span>layouts<span class="token operator">/</span>app<span class="token operator">.</span>blade<span class="token operator">.</span>php <span class="token operator">--</span><span class="token operator">></span></span>
<span class="line"></span>
<span class="line"><span class="token operator">&lt;</span>html<span class="token operator">></span></span>
<span class="line">    <span class="token operator">&lt;</span>head<span class="token operator">></span></span>
<span class="line">        <span class="token operator">&lt;</span>title<span class="token operator">></span>应用程序名称 <span class="token operator">-</span> @<span class="token keyword">yield</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'title'</span><span class="token punctuation">)</span><span class="token operator">&lt;</span><span class="token operator">/</span>title<span class="token operator">></span></span>
<span class="line">    <span class="token operator">&lt;</span><span class="token operator">/</span>head<span class="token operator">></span></span>
<span class="line">    <span class="token operator">&lt;</span>body<span class="token operator">></span></span>
<span class="line">        @<span class="token function">section</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sidebar'</span><span class="token punctuation">)</span></span>
<span class="line">            这是主布局的侧边栏。</span>
<span class="line">        @show</span>
<span class="line"></span>
<span class="line">        <span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string double-quoted-string">"container"</span><span class="token operator">></span></span>
<span class="line">            @<span class="token keyword">yield</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'content'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line">    <span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">></span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>html<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如你所见，该文件包含了典型的 HTML 语法。不过，请注意 <code v-pre>@section</code> 和 <code v-pre>@yield</code> 命令。顾名思义，<code v-pre>@section</code> 命令定义了视图的一部分内容，而 <code v-pre>@yield</code> 指令是用来显示指定部分的内容。</p>
<p>现在，我们已经定义好了这个应用程序的布局，接下来，我们定义一个继承此布局的子页面。</p>
<h3 id="继承布局" tabindex="-1"><a class="header-anchor" href="#继承布局"><span>继承布局</span></a></h3>
<p>当定义子视图时，你可以使用 Blade 提供的 <code v-pre>@extends</code> 命令来为子视图指定应该 「继承」 的布局。 继承 Blade 布局的视图可使用 <code v-pre>@section</code> 命令将内容注入于布局的 <code v-pre>@section</code> 中。而「主」布局中使用 <code v-pre>@yield</code> 的地方会显示这些子视图中的 <code v-pre>@section</code> 间的内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 文件保存于 resources<span class="token operator">/</span>views<span class="token operator">/</span>layouts<span class="token operator">/</span>child<span class="token operator">.</span>blade<span class="token operator">.</span>php <span class="token operator">--</span><span class="token operator">></span></span>
<span class="line"></span>
<span class="line">@<span class="token keyword">extends</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'layouts.app'</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">section</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Page Title'</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">section</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sidebar'</span><span class="token punctuation">)</span></span>
<span class="line">    @<span class="token keyword">parent</span></span>
<span class="line"></span>
<span class="line">    <span class="token operator">&lt;</span>p<span class="token operator">></span>这将追加到主布局的侧边栏。<span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">></span></span>
<span class="line">@endsection</span>
<span class="line"></span>
<span class="line">@<span class="token function">section</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'content'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">&lt;</span>p<span class="token operator">></span>这是主体内容。<span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">></span></span>
<span class="line">@endsection</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在上面的例子里，<code v-pre>@section</code> 中的 <code v-pre>sidebar</code> 使用 <code v-pre>@parent</code> 命令在「主」布局的 <code v-pre>@section('sidebar')</code> 中增加内容（不是覆盖）。渲染视图时，<code v-pre>@parent</code> 指令会被替换为「主」布局中 <code v-pre>@section('sidebar')</code> 间的内容。</p>
<blockquote>
<p>{tip} 与上一个示例相反，此侧边栏部分以 <code v-pre>@endsection</code> 而不是 <code v-pre>@show</code> 结尾。 <code v-pre>@endsection</code> 指令只定义一个区块，而 <code v-pre>@show</code> 则是定义并立即生成该区块。</p>
</blockquote>
<p>你也可以通过在路由中使用全局辅助函数 <code v-pre>view</code> 来返回 Blade 视图：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'blade'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="components-slots" tabindex="-1"><a class="header-anchor" href="#components-slots"><span>Components &amp; Slots</span></a></h2>
<p>Components 和 slots 类似于布局中的 <code v-pre>@section</code>，但其使用方式更容易使人理解。首先，假设我们有一个能在整个应用程序中被重复使用的「警告」组件:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> <span class="token operator">/</span>resources<span class="token operator">/</span>views<span class="token operator">/</span>alert<span class="token operator">.</span>blade<span class="token operator">.</span>php <span class="token operator">--</span><span class="token operator">></span></span>
<span class="line"></span>
<span class="line"><span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string double-quoted-string">"alert alert-danger"</span><span class="token operator">></span></span>
<span class="line">    <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$slot</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>{{ $slot }}</code> 变量将包含我们希望注入到组件的内容。然后，我们可以使用 Blade 命令 <code v-pre>@component</code> 来构建这个组件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'alert'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">&lt;</span>strong<span class="token operator">></span>Whoops<span class="token operator">!</span><span class="token operator">&lt;</span><span class="token operator">/</span>strong<span class="token operator">></span> Something went wrong<span class="token operator">!</span></span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有时为组件定义多个 slots 是很有帮助的。现在我们要对「警报」组件进行修改，让它可以注入「标题」。通过简单地 「打印」匹配其名称的变量来显示被命名的 <code v-pre>@slot</code> 之间的内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> <span class="token operator">/</span>resources<span class="token operator">/</span>views<span class="token operator">/</span>alert<span class="token operator">.</span>blade<span class="token operator">.</span>php <span class="token operator">--</span><span class="token operator">></span></span>
<span class="line"></span>
<span class="line"><span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string double-quoted-string">"alert alert-danger"</span><span class="token operator">></span></span>
<span class="line">    <span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string double-quoted-string">"alert-title"</span><span class="token operator">></span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$title</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span>
<span class="line">    <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$slot</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>现在，我们可以使用 <code v-pre>@slot</code> 指令注入内容到已命名的 slot 中，任何没有被 <code v-pre>@slot</code> 指令包裹住的内容将传递给组件中的 <code v-pre>$slot</code> 变量:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'alert'</span><span class="token punctuation">)</span></span>
<span class="line">    @<span class="token function">slot</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'title'</span><span class="token punctuation">)</span></span>
<span class="line">        Forbidden</span>
<span class="line">    @endslot</span>
<span class="line"></span>
<span class="line">    你没有权限访问这个资源！</span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="传递额外的数据给组件" tabindex="-1"><a class="header-anchor" href="#传递额外的数据给组件"><span>传递额外的数据给组件</span></a></h4>
<p>有时候你可能需要传递额外的数据给组件。你可以传递一个数组作为第二个参数传递给 <code v-pre>@component</code> 指令。所有的数据都将以变量的形式传递给组件模版:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'alert'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line">@endcomponent</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="显示数据" tabindex="-1"><a class="header-anchor" href="#显示数据"><span>显示数据</span></a></h2>
<p>你可以使用 「中括号」 包住变量将数据传递给 Blade 视图。如下面的路由设置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'greeting'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'welcome'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Samantha'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以像这样显示 <code v-pre>name</code> 变量的内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">Hello<span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$name</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">.</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>当然，不仅仅只能用传递数据的方式让视图来显示变量内容。你也可以打印 PHP 函数的结果。其实，你可以在 Blade 打印语法中放置任何 PHP 代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">The current <span class="token constant">UNIX</span> timestamp is <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">.</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>{note} Blade 的 <code v-pre>{{ }}</code> 语法会自动调用 PHP <code v-pre>htmlspecialchars</code> 函数来避免 XSS 攻击。</p>
</blockquote>
<h4 id="显示未转义的数据" tabindex="-1"><a class="header-anchor" href="#显示未转义的数据"><span>显示未转义的数据</span></a></h4>
<p>默认情况下，Blade 的 <code v-pre>{{ }}</code> 语法将会自动调用 PHP <code v-pre>htmlspecialchars</code> 函数来避免 XSS 攻击。如果你不想你的数据被转义，你可以使用下面的语法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">Hello<span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token operator">!</span><span class="token operator">!</span> <span class="token variable">$name</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">}</span><span class="token operator">.</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>{note} 处理用户输入的数据时要非常小心。在显示用户提供的数据时，你应该始终使用转义的 <code v-pre>{{ }}</code> 语法来防止 XSS 攻击。</p>
</blockquote>
<h4 id="渲染-json" tabindex="-1"><a class="header-anchor" href="#渲染-json"><span>渲染 JSON</span></a></h4>
<p>当你将数组传递给视图时，会将数组转化成 JSON 数据，以此来初始化 JavaScript 变量。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"></span>
<span class="line">    <span class="token keyword">var</span> app <span class="token operator">=</span> <span class="token php language-php"><span class="token delimiter important">&lt;?php</span> <span class="token function">json_encode</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span><span class="token punctuation">;</span></span>
<span class="line"></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 Blade 指令 <code v-pre>@json</code> 来代替手动调用 <code v-pre>json_encode</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>script<span class="token operator">></span></span>
<span class="line">    <span class="token keyword">var</span> app <span class="token operator">=</span> @<span class="token function">json</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="blade-javascript-框架" tabindex="-1"><a class="header-anchor" href="#blade-javascript-框架"><span>Blade &amp; JavaScript 框架</span></a></h3>
<p>由于很多 JavaScript 框架都使用花括号来表示给定的表达式应该在浏览器中显示，你可以使用 <code v-pre>@</code> 符号来告知 Blade 渲染引擎你需要保留这个表达式原始形态，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>h1<span class="token operator">></span>Laravel<span class="token operator">&lt;</span><span class="token operator">/</span>h1<span class="token operator">></span></span>
<span class="line"></span>
<span class="line">Hello<span class="token punctuation">,</span> @<span class="token punctuation">{</span><span class="token punctuation">{</span> name <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">.</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在这个例子里，<code v-pre>@</code> 符号最终会被 Blade 引擎删除，达到不受 Blade 模板引擎影响的目的，最终 <code v-pre>{{ name }}</code> 表达式会保持不变使得 JavaScript 框架可以使用它。</p>
<h4 id="verbatim-指令" tabindex="-1"><a class="header-anchor" href="#verbatim-指令"><span><code v-pre>@verbatim</code> 指令</span></a></h4>
<p>如果你需要在页面中大部分内容中展示 JavaScript 变量，你可以使用 <code v-pre>@verbatim</code> 指令来包裹 HTML 内容，这样你就不必在每个Blade 打印语句前加上 <code v-pre>@</code> 符号：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@verbatim</span>
<span class="line">    <span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string double-quoted-string">"container"</span><span class="token operator">></span></span>
<span class="line">        Hello<span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> name <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">.</span></span>
<span class="line">    <span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line">@endverbatim</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="流程控制" tabindex="-1"><a class="header-anchor" href="#流程控制"><span>流程控制</span></a></h2>
<p>除了模板继承与数据显示的功能以外，Blade 还提供了常见的 PHP 流程控制语句，比如条件表达式和循环语句。这些语句与 PHP 语句的相似，与其一样清晰简明。</p>
<h3 id="if-语句" tabindex="-1"><a class="header-anchor" href="#if-语句"><span>If 语句</span></a></h3>
<p>你可以使用 <code v-pre>@if</code>、<code v-pre>@elseif</code>、<code v-pre>@else</code> 及 <code v-pre>@endif</code> 指令来构建 <code v-pre>if</code> 表达式。这些命令的功能等同于 PHP 中的语法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token variable">$records</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token number">1</span><span class="token punctuation">)</span></span>
<span class="line">    我有一条记录！</span>
<span class="line">@<span class="token keyword">elseif</span> <span class="token punctuation">(</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token variable">$records</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">)</span></span>
<span class="line">    我有多条记录！</span>
<span class="line">@<span class="token keyword">else</span></span>
<span class="line">    我没有任何记录！</span>
<span class="line">@<span class="token keyword">endif</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>为了方便，Blade 还提供了一个 <code v-pre>@unless</code> 命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">unless</span> <span class="token punctuation">(</span><span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">check</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    你尚未登录。</span>
<span class="line">@endunless</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>除了以上的条件指令之外，<code v-pre>@isset</code> 和 <code v-pre>@empty</code> 指令也可以视为与 PHP 函数有相同的功能：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token keyword">isset</span><span class="token punctuation">(</span><span class="token variable">$records</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token comment">// $records 被定义并且不为空...</span></span>
<span class="line">@endisset</span>
<span class="line"></span>
<span class="line">@<span class="token keyword">empty</span><span class="token punctuation">(</span><span class="token variable">$records</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token comment">// $records 是「空」的...</span></span>
<span class="line">@endempty</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="身份验证快捷方式" tabindex="-1"><a class="header-anchor" href="#身份验证快捷方式"><span>身份验证快捷方式</span></a></h4>
<p><code v-pre>@auth</code> 和 <code v-pre>@guest</code> 指令可以用来快速确定当前用户是否已通过身份验证，是否为访客：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@auth</span>
<span class="line">    <span class="token comment">// 用户已经通过身份认证...</span></span>
<span class="line">@endauth</span>
<span class="line"></span>
<span class="line">@guest</span>
<span class="line">    <span class="token comment">// 用户没有通过身份认证...</span></span>
<span class="line">@endguest</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="switch-语句" tabindex="-1"><a class="header-anchor" href="#switch-语句"><span>Switch 语句</span></a></h3>
<p>可以使用 <code v-pre>@switch</code>、<code v-pre>@case</code>、<code v-pre>@break</code>、<code v-pre>@default</code> 和 <code v-pre>@endswitch</code> 指令来构建 Switch 语句：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token keyword">switch</span><span class="token punctuation">(</span><span class="token variable">$i</span><span class="token punctuation">)</span></span>
<span class="line">    @<span class="token keyword">case</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span></span>
<span class="line">        First <span class="token keyword">case</span><span class="token operator">...</span></span>
<span class="line">        @<span class="token keyword">break</span></span>
<span class="line"></span>
<span class="line">    @<span class="token keyword">case</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span></span>
<span class="line">        Second <span class="token keyword">case</span><span class="token operator">...</span></span>
<span class="line">        @<span class="token keyword">break</span></span>
<span class="line"></span>
<span class="line">    @<span class="token keyword">default</span></span>
<span class="line">        <span class="token keyword">Default</span> <span class="token keyword">case</span><span class="token operator">...</span></span>
<span class="line">@<span class="token keyword">endswitch</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="循环" tabindex="-1"><a class="header-anchor" href="#循环"><span>循环</span></a></h3>
<p>除了条件表达式外，Blade 也支持 PHP 的循环结构。同样，以下这些指令中的每一个都与其 PHP 对应的函数相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token variable">$i</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token variable">$i</span> <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> <span class="token variable">$i</span><span class="token operator">++</span><span class="token punctuation">)</span></span>
<span class="line">    目前的值为 <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$i</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line">@<span class="token keyword">endfor</span></span>
<span class="line"></span>
<span class="line">@<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">&lt;</span>p<span class="token operator">></span>此用户为 <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">></span></span>
<span class="line">@<span class="token keyword">endforeach</span></span>
<span class="line"></span>
<span class="line">@<span class="token function">forelse</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">&lt;</span>li<span class="token operator">></span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span></span>
<span class="line">@<span class="token keyword">empty</span></span>
<span class="line">    <span class="token operator">&lt;</span>p<span class="token operator">></span>没有用户<span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">></span></span>
<span class="line">@endforelse</span>
<span class="line"></span>
<span class="line">@<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">&lt;</span>p<span class="token operator">></span>死循环了。<span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">></span></span>
<span class="line">@<span class="token keyword">endwhile</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{tip} 循环时，你可以使用 <a href="#the-loop-variable">循环变量</a> 来获取循环的信息，例如是否在循环中进行第一次或最后一次迭代。</p>
</blockquote>
<p>当使用循环时，你也可以结束循环或跳过当前迭代：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">    @<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">type</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span></span>
<span class="line">        @<span class="token keyword">continue</span></span>
<span class="line">    @<span class="token keyword">endif</span></span>
<span class="line"></span>
<span class="line">    <span class="token operator">&lt;</span>li<span class="token operator">></span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span></span>
<span class="line"></span>
<span class="line">    @<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">number</span> <span class="token operator">==</span> <span class="token number">5</span><span class="token punctuation">)</span></span>
<span class="line">        @<span class="token keyword">break</span></span>
<span class="line">    @<span class="token keyword">endif</span></span>
<span class="line">@<span class="token keyword">endforeach</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以使用一行代码包含指令声明的条件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">    @<span class="token keyword">continue</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">type</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">    <span class="token operator">&lt;</span>li<span class="token operator">></span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">></span></span>
<span class="line"></span>
<span class="line">    @<span class="token keyword">break</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">number</span> <span class="token operator">==</span> <span class="token number">5</span><span class="token punctuation">)</span></span>
<span class="line">@<span class="token keyword">endforeach</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="循环变量" tabindex="-1"><a class="header-anchor" href="#循环变量"><span>循环变量</span></a></h3>
<p>循环时，可以在循环内使用 <code v-pre>$loop</code> 变量。这个变量可以提供一些有用的信息，比如当前循环的索引，当前循环是不是首次迭代，又或者当前循环是不是最后一次迭代：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">    @<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$loop</span><span class="token operator">-></span><span class="token property">first</span><span class="token punctuation">)</span></span>
<span class="line">        这是第一个迭代。</span>
<span class="line">    @<span class="token keyword">endif</span></span>
<span class="line"></span>
<span class="line">    @<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$loop</span><span class="token operator">-></span><span class="token property">last</span><span class="token punctuation">)</span></span>
<span class="line">        这是最后一个迭代。</span>
<span class="line">    @<span class="token keyword">endif</span></span>
<span class="line"></span>
<span class="line">    <span class="token operator">&lt;</span>p<span class="token operator">></span>This is user <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">></span></span>
<span class="line">@<span class="token keyword">endforeach</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在一个嵌套的循环中，可以通过使用 <code v-pre>$loop</code> 变量的 <code v-pre>parent</code> 属性来获取父循环中的 <code v-pre>$loop</code> 变量：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">    @<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">posts</span> <span class="token keyword">as</span> <span class="token variable">$post</span><span class="token punctuation">)</span></span>
<span class="line">        @<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$loop</span><span class="token operator">-></span><span class="token property">parent</span><span class="token operator">-></span><span class="token property">first</span><span class="token punctuation">)</span></span>
<span class="line">            This is first iteration of the <span class="token keyword">parent</span> loop<span class="token operator">.</span></span>
<span class="line">        @<span class="token keyword">endif</span></span>
<span class="line">    @<span class="token keyword">endforeach</span></span>
<span class="line">@<span class="token keyword">endforeach</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>$loop</code> 变量也包含了其它各种有用的属性：</p>
<table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$loop-&gt;index</code></td>
<td>当前循环迭代的索引（从0开始）。</td>
</tr>
<tr>
<td><code v-pre>$loop-&gt;iteration</code></td>
<td>当前循环迭代 （从1开始）。</td>
</tr>
<tr>
<td><code v-pre>$loop-&gt;remaining</code></td>
<td>循环中剩余迭代数量。</td>
</tr>
<tr>
<td><code v-pre>$loop-&gt;count</code></td>
<td>迭代中的数组项目总数。</td>
</tr>
<tr>
<td><code v-pre>$loop-&gt;first</code></td>
<td>当前迭代是否是循环中的首次迭代。</td>
</tr>
<tr>
<td><code v-pre>$loop-&gt;last</code></td>
<td>当前迭代是否是循环中的最后一次迭代。</td>
</tr>
<tr>
<td><code v-pre>$loop-&gt;depth</code></td>
<td>当前循环的嵌套级别。</td>
</tr>
<tr>
<td><code v-pre>$loop-&gt;parent</code></td>
<td>在嵌套循环中，父循环的变量。</td>
</tr>
</tbody>
</table>
<h3 id="注释" tabindex="-1"><a class="header-anchor" href="#注释"><span>注释</span></a></h3>
<p>Blade 也能在视图中定义注释。然而，跟 HTML 的注释不同的，Blade 注释不会被包含在应用程序返回的 HTML 内：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span><span class="token operator">--</span> 此注释将不会出现在渲染后的 <span class="token constant">HTML</span> <span class="token operator">--</span><span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="php" tabindex="-1"><a class="header-anchor" href="#php"><span>PHP</span></a></h3>
<p>在某些情况下，将 PHP 代码嵌入到视图中很有用。你可以使用 Blade 的 <code v-pre>@php</code> 指令在模板中执行一段纯 PHP 代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@php</span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line">@endphp</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{tip} 虽然 Blade 提供了这个功能，但频繁地使用意味着你的模版被嵌入了太多的逻辑。</p>
</blockquote>
<h2 id="引入子视图" tabindex="-1"><a class="header-anchor" href="#引入子视图"><span>引入子视图</span></a></h2>
<p>你可以使用 Blade 的 <code v-pre>@include</code> 命令来引入一个已存在的视图，所有在父视图的可用变量在被引入的视图中都是可用的。</p>
<p>使用 Blade 的 <code v-pre>@include</code> 指令可以在 Blade 视图中引入另一个视图。父视图可用的所有变量将提供给引入的视图：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>div<span class="token operator">></span></span>
<span class="line">    @<span class="token keyword">include</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'shared.errors'</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">    <span class="token operator">&lt;</span>form<span class="token operator">></span></span>
<span class="line">        <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> Form Contents <span class="token operator">--</span><span class="token operator">></span></span>
<span class="line">    <span class="token operator">&lt;</span><span class="token operator">/</span>form<span class="token operator">></span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>被引入的视图会继承父视图中的所有数据，同时也可以向引入的视图传递额外的数组数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token keyword">include</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'some'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>当然，如果尝试使用 <code v-pre>@include</code> 去引入一个不存在的视图，Laravel 会抛出错误。如果想引入一个可能存在或可能不存在的视图，就使用 <code v-pre>@includeIf</code> 指令:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">includeIf</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'some'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果要根据给定的布尔条件 <code v-pre>@include</code> 视图，可以使用 <code v-pre>@includeWhen</code> 指令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">includeWhen</span><span class="token punctuation">(</span><span class="token variable">$boolean</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'view.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'some'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>{note} 请避免在 Blade 视图中使用 <code v-pre>__DIR__</code> 及 <code v-pre>__FILE__</code> 常量，因为它们会引用编译视图时缓存的位置。</p>
</blockquote>
<h3 id="为集合渲染视图" tabindex="-1"><a class="header-anchor" href="#为集合渲染视图"><span>为集合渲染视图</span></a></h3>
<p>你可以使用 Blade 的 <code v-pre>@each</code> 命令将循环及引入写成一行代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">each</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view.name'</span><span class="token punctuation">,</span> <span class="token variable">$jobs</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'job'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>第一个参数是对数组或集合中的每个元素进行渲染的部分视图。第二个参数是要迭代的数组或集合，而第三个参数是将被分配给视图中当前迭代的变量名称。举个例子，如果你要迭代一个 <code v-pre>jobs</code> 数组，通常会使用子视图中的变量 <code v-pre>job</code> 来获取每个 job。当前迭代的 <code v-pre>key</code> 将作为子视图中的 <code v-pre>key</code> 变量。</p>
<p>你也可以传递第四个参数到 <code v-pre>@each</code> 命令。当需要迭代的数组为空时，将会使用这个参数提供的视图来渲染。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">each</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view.name'</span><span class="token punctuation">,</span> <span class="token variable">$jobs</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'job'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'view.empty'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>{note} 通过 <code v-pre>@each</code> 渲染的视图不会从父视图继承变量。 如果子视图需要这些变量，则应该使用 <code v-pre>@foreach</code> 和 <code v-pre>@include</code>。</p>
</blockquote>
<h2 id="堆栈" tabindex="-1"><a class="header-anchor" href="#堆栈"><span>堆栈</span></a></h2>
<p>Blade 可以被推送到在其他视图或布局中的其他位置渲染的命名堆栈。这在子视图中指定所需的 JavaScript 库时非常有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">push</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'scripts'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">&lt;</span>script src<span class="token operator">=</span><span class="token string double-quoted-string">"/example.js"</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">></span></span>
<span class="line">@endpush</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以根据需要多次压入堆栈，通过 <code v-pre>@stack</code> 命令中传递堆栈的名称来渲染完整的堆栈内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>head<span class="token operator">></span></span>
<span class="line">    <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> Head Contents <span class="token operator">--</span><span class="token operator">></span></span>
<span class="line"></span>
<span class="line">    @<span class="token function">stack</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'scripts'</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>head<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="服务注入" tabindex="-1"><a class="header-anchor" href="#服务注入"><span>服务注入</span></a></h2>
<p><code v-pre>@inject</code> 命令可用于从 Laravel <a href="https://learnku.com/docs/laravel/5.5/container" target="_blank" rel="noopener noreferrer">服务容器</a> 中检索服务。传递给 <code v-pre>@inject</code> 的第一个参数为置放该服务的变量名称，而第二个参数是要解析的服务的类或是接口的名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">inject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'metrics'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'App\Services\MetricsService'</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line"><span class="token operator">&lt;</span>div<span class="token operator">></span></span>
<span class="line">    Monthly Revenue<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$metrics</span><span class="token operator">-></span><span class="token function">monthlyRevenue</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">.</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="拓展-blade" tabindex="-1"><a class="header-anchor" href="#拓展-blade"><span>拓展 Blade</span></a></h2>
<p>Blade 甚至允许你使用 <code v-pre>directive</code> 方法来定义自定义指令。当 Blade 编译器遇到自定义指令时，它将使用指令包含的表达式调用提供的回调。</p>
<p>以下示例创建一个 <code v-pre>@datetime($var)</code> 伪指令，该伪指令格式化给定的 <code v-pre>DateTime</code> 实例 <code v-pre>$var</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Blade</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">AppServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 执行注册后引导服务。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Blade</span><span class="token operator">::</span><span class="token function">directive</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'datetime'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$expression</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token string double-quoted-string">"&lt;?php echo (<span class="token interpolation"><span class="token variable">$expression</span></span>)->format('m/d/Y H:i'); ?>"</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 在容器中注册绑定。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">//</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如你所见，我们可以链式调用在指令中传递的任何表达式的 <code v-pre>format</code> 方法。所以，在这个例子里，该指令最终生成了以下 PHP 代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span> <span class="token keyword">echo</span> <span class="token punctuation">(</span><span class="token variable">$var</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'m/d/Y H:i'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter important">?></span></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>{note} 更新 Blade 指令的逻辑后，你需要删除所有已缓存的 Blade 视图。使用 Artisan 命令 <code v-pre>view:clear</code> 来清除被缓存的视图。</p>
</blockquote>
<h3 id="自定义-if-语句" tabindex="-1"><a class="header-anchor" href="#自定义-if-语句"><span>自定义 If 语句</span></a></h3>
<p>编写自定义指令有时候比定义简单、常见的条件语句更复杂，但是它又非常必要。因此，Blade 提供了一个 <code v-pre>Blade::if</code> 方法，它能使用闭包来快速定义自定义条件指令。 例如，定义一个自定义条件来检查当前的应用程序环境。我们可以在 <code v-pre>AppServiceProvider</code> 的 <code v-pre>boot</code> 方法中这样做：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Blade</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Perform post-registration booting of services.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Blade</span><span class="token operator">::</span><span class="token function">if</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'env'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$environment</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">app</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">environment</span><span class="token punctuation">(</span><span class="token variable">$environment</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>定义了自定义条件之后，就可以很轻松地在模板中使用它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">@<span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'local'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token comment">// The application is in the local environment...</span></span>
<span class="line">@<span class="token keyword">else</span></span>
<span class="line">    <span class="token comment">// The application is not in the local environment...</span></span>
<span class="line">@endenv</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://github.com/boxshadow" target="_blank" rel="noopener noreferrer">Wayne John</a></td>
<td><img src="https://avatars1.githubusercontent.com/u/8577474?s=100" alt="8577474?s=100"></td>
<td>翻译</td>
<td>基于 Laravel 的社交开源系统 <a href="https://github.com/slimkit/thinksns-plus" target="_blank" rel="noopener noreferrer">ThinkSNS+</a> 欢迎 Star。</td>
</tr>
<tr>
<td><a href="https://learnku.com/users/5350" target="_blank" rel="noopener noreferrer">@JokerLinly</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/5350_1481857380.jpg" alt="5350_1481857380.jpg"></td>
<td>Review</td>
<td>Stay Hungry. Stay Foolish.</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


